<template>
  <comcard>
    <div slot="title_name">管理员信息</div>
    <div slot="body_name">
      <div class="qqq">
        <div>管理员ID：{{ accountInfo.id }}</div>
        <div>账号：{{ accountInfo.account }}</div>
        <div>用户组：{{ accountInfo.userGroup }}</div>
        <div>创建时间：{{ accountInfo.ctime | timeMF }}</div>
        <el-upload
          class="avatar-uploader"
          action="http://localhost:5000/users/avatar_upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <div>
          <el-button type="primary" @click="save()">修改头像</el-button>
        </div>
      </div>
    </div>
  </comcard>
</template>

<script>
import comcard from "../../components/comcard/index.vue";
import { api_findOne, api_upimg } from "../../api/users";
export default {
  data() {
    return {
      accountInfo: {},

      imageUrl: "",
      srcurl: "",
    };
  },
  components: {
    comcard,
  },
  mounted() {
    this.query();
  },
  methods: {
    async query() {
      let rst = await api_findOne();
      console.log(rst);
      let { accountInfo } = rst;
      this.accountInfo = accountInfo;
      this.imageUrl = accountInfo.imgUrl;
      // this.logcal.set("accountInfo", accountInfo);
    },
    async save() {
      let rst = await api_upimg({ imgUrl: this.srcurl });
      console.log(rst);
      this.$bus.$emit("upimg");
    },

    handleAvatarSuccess(res, file) {
      this.srcurl = res.imgUrl;
      this.imageUrl = `http://localhost:5000/upload/imgs/acc_img/${res.imgUrl}`;
      console.log(res);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
div {
  margin-top: 20px;
}

/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>